#include "informationcontrol.h"
#include "ui_informationcontrol.h"

InformationControl::InformationControl(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::InformationControl),
    expFile(""), imgFolder(""), included(false),
    start(QDateTime::currentDateTime()), interval(1)
{
    m_ui->setupUi(this);
    m_ui->dateTimeEdit->setCalendarPopup(true);
    m_ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    m_ui->dateTimeEdit->setDisplayFormat("MMMM dd yyyy hh:mm:ss");

    connect(m_ui->browseButton, SIGNAL(clicked()), this, SLOT(browse()));
    connect(m_ui->locateButton, SIGNAL(clicked()), this, SLOT(locate()));
    connect(m_ui->yesCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setYes(int)));
    connect(m_ui->dateTimeEdit, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(setDateTime(QDateTime)));
    connect(m_ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(setInterval(int)));
    connect(m_ui->horizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(showVal(int)));
    connect(m_ui->okButton, SIGNAL(clicked()), this, SLOT(ok()));
    connect(m_ui->cancelButton, SIGNAL(clicked()), this, SLOT(close()));
}

InformationControl::~InformationControl()
{
    delete m_ui;
}

void InformationControl::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void InformationControl::browse()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Experiment file"), "/home",
                                                    tr("Experiment Files (*.txt *sas)"));

    if(fileName.length() > 0)
    {
        expFile = fileName;
        m_ui->fileEdit->setText(fileName);
    }
}
void InformationControl::locate()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open image folder"), "/home", QFileDialog::ShowDirsOnly);

    if(dir.length() > 0)
    {
        imgFolder = dir;
        m_ui->dirEdit->setText(dir);
    }
}

QString InformationControl::getFile()
{
    return expFile;
}

QString InformationControl::getDir()
{
    return imgFolder;
}

bool InformationControl::isIncluded()
{
    return included;
}

void InformationControl::setYes(int state)
{
    if(state == Qt::Checked)
    {
        included = true;
    }
    else if(state == Qt::Unchecked)
        included = false;
}

void InformationControl::setDateTime(QDateTime dt)
{
    QDateTime now = QDateTime::currentDateTime();
    if(now > dt)
        start = dt;
}

QDateTime InformationControl::getStartDateTime()
{
    return start;
}

void InformationControl::setInterval(int interval)
{
    this->interval = interval;
}

int InformationControl::getInterval()
{
    return interval;
}

void InformationControl::showVal(int val)
{
    m_ui->intervalLabel->setText(QString::number(val).append(tr("h")));
}

void InformationControl::ok()
{
    if(expFile.length() > 0 && imgFolder.length() > 0 &&
       start.isValid())
    {
        emit(finished(1));
        this->deleteLater();
    }
    else
    {
        QMessageBox::warning(this, "Empty fields",
                             "Some fields have not entered");
    }
}

void InformationControl::close()
{
    emit(finished(0));
    this->deleteLater();
}
